#include <glad/glad.h>

#include <cuda_gl_interop.h>
#include <cuda_runtime.h>

#include "CudaColorBar.cuh"
#include <algorithm/rayTracing/qmcrt_util/check_cuda.h>

using namespace solar::cuda;

CudaColorBar::CudaColorBar() : colors_(new int3[6]), factors_(new float[6])
{
     // 这个5是自己设置的，颜色的数目-------或许可以换成其他方式
    
    // cudaCheckErrors(cudaMalloc((void**)&colors_, (num+1)*sizeof(int3)));
    // cudaCheckErrors(cudaMalloc((void**)&factors_, (num+1)*sizeof(float)));
}
CudaColorBar::~CudaColorBar()
{
    delete[] colors_;
    delete[] factors_;
    // cudaFree(colors_);
    // cudaFree(factors_);
}

void CudaColorBar::setColor()
{
    // std::cout << "set color\n";
    int num = 8;
    int i = 0;
    int3 color;
    while (i <= num)
    {
        // colors_value_.push_back((max_ - min_) / num * i)
        if (i == 0 || i == 8)
        {
            if (i == 0)
                color = make_int3(0, 0, 0.5 * 255);
            else
                color = make_int3(0.5 * 255, 0, 0);
            float factor = (float)(i) / num;
            // printf("i=%d,factor=%f, color=(%d, %d, %d)\n", i, factor, color.x, color.y, color.z);
            colors_[color_num_] = color;
            factors_[color_num_] = factor;
            color_num_++;
            i++;
        }
        else
        {
            float r = i == 5 || i == 7 ? 1.0 : 0.0;
            float g = i == 3 || i == 5 ? 1.0 : 0.0;
            float b = i == 1 || i == 3 ? 1.0 : 0.0;
            color = make_int3(r * 255, g * 255, b * 255);
            float factor = (float)(i) / num;
            // printf("i=%d,factor=%f, color=(%d, %d, %d)\n", i, factor, color.x, color.y, color.z);
            colors_[color_num_] = color;
            factors_[color_num_] = factor;
            color_num_++;
            if (i == 7)
                i++;
            else
                i += 2;
        }
    }
}
void CudaColorBar::test()
{
    printf("---------CudaColorBar test begin----------\n");
    for (int i = 0; i < color_num_; i++)
    {
        printf("i=%d, factor=%f, color=(%d,%d,%d)\n", i, factors_[i], colors_[i].x, colors_[i].y,
               colors_[i].z);
    }
    printf("---------CudaColorBar test end------------\n");
}

// rgb8_pixel_t ColorBar::getColorAtValue(float value)
// {
//     float factor = getFactor(value);
//     auto ret = getColorAtFactor(factor);

//     return ret;
// }
// rgb8_pixel_t ColorBar::getColorAtValue(float value, float min_value, float max_value)
// {
//     float factor = (value - min_value) / (max_value - min_value);
//     auto ret = getColorAtFactor(factor);

//     return ret;
// }
// rgb8_pixel_t ColorBar::getColorAtFactor(float factor)
// {
//     int lower_index =  std::lower_bound(factors_.begin(), factors_.end(), factor) -
//     factors_.begin(); int upper_index = std::upper_bound(factors_.begin(), factors_.end(),
//     factor) - factors_.begin();

//     if(lower_index < upper_index) // 表示有一个等于factor
//     {
//         int index = lower_index;
//         return colors_[index];
//     }
//     int left_index = lower_index-1, right_index=lower_index;
//     float coeff = (factor - factors_[left_index]) / (factors_[right_index] -
//     factors_[left_index]) ; auto left_color = colors_[left_index]; auto right_color =
//     colors_[right_index];

//     rgb8_pixel_t ret ;
//     ret[0] = left_color[0] + (right_color[0] - left_color[0]) * coeff;
//     ret[1] = left_color[1] + (right_color[1] - left_color[1]) * coeff;
//     ret[2] = left_color[2] + (right_color[2] - left_color[2]) * coeff;

//     return ret;
// }
